package com.warmheart.impl.sys;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.warmheart.base.config.WhTransactionalControl;
import com.warmheart.base.model.EntityModel;
import com.warmheart.base.service.impl.BaseService;
import com.warmheart.core.enums.DELETED;
import com.warmheart.core.enums.PlatEnum;
import com.warmheart.core.util.EntityCommonCopyUtil;
import com.warmheart.core.util.JsonUtil;
import com.warmheart.dao.sys.SysConfigDao;
import com.warmheart.entity.sys.SysConfig;
import com.warmheart.model.ConfigModel;
import com.warmheart.service.sys.ISysConfigService;

/**
 * 
 * @ClassName: SysConfigService
 * @Description: 参数配置Service，对应表名称为tb_sys_config
 *               温馨提示：对外提供的接口，原则上这里只写自定义方法
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 */
@Service(value = "sysConfigService")
public class SysConfigService extends BaseService<SysConfig> implements ISysConfigService {

    private static Logger logger = LoggerFactory.getLogger(SysConfigService.class);

    @Autowired
    private SysConfigDao sysConfigDao;

    @Override
    public SysConfigDao getDao() {
        return this.sysConfigDao;
    }

    @Override
    public String getString(PlatEnum.SysConfigEnum key) {
        SysConfig paramSysConfig = new SysConfig();
        paramSysConfig.setConfigKey(key.getValue());
        paramSysConfig = this.getDao().selectOne(paramSysConfig);
        if (paramSysConfig == null) {
            return null;
        }
        return paramSysConfig.getConfigValue();
    }

    @Override
    public <E> E getModel(PlatEnum.SysConfigEnum key, Class<E> cls) {
        SysConfig paramSysConfig = new SysConfig();
        paramSysConfig.setConfigKey(key.getValue());
        paramSysConfig = this.getDao().selectOne(paramSysConfig);
        if (paramSysConfig == null) {
            return null;
        }
        String value = paramSysConfig.getConfigValue();
        if (StringUtils.isBlank(value)) {
            return null;
        }
        E model = null;
        try {
            model = JsonUtil.getObject(value, cls);
        } catch (Exception e) {
            logger.error("获取配置模型数据格式转换异常", e);
        }
        return model;
    }

    @Override
    @WhTransactionalControl
    public void setString(PlatEnum.SysConfigEnum key, String value, EntityModel... entityModels) throws Exception {
        if (StringUtils.isBlank(value)) {
            return;
        }
        Date now = new Date();
        SysConfig checkSysConfig = new SysConfig();
        checkSysConfig.setConfigKey(key.getValue());
        checkSysConfig = sysConfigDao.selectOne(checkSysConfig);
        if (checkSysConfig != null) {
            SysConfig sysConfig = new SysConfig();
            sysConfig.setId(checkSysConfig.getId());
            sysConfig.setConfigValue(value);
            if (entityModels != null && entityModels.length > 0 && entityModels[0] != null) {
                EntityCommonCopyUtil.copyConfig2Update(entityModels[0], sysConfig);
            } else {
                sysConfig.setCstModify(now);
            }
            sysConfigDao.updateBySelective(sysConfig);
        } else {
            SysConfig sysConfig = new SysConfig();
            sysConfig.setConfigKey(key.getValue());
            sysConfig.setConfigValue(value);
            if (entityModels != null && entityModels.length > 0 && entityModels[0] != null) {
                EntityCommonCopyUtil.copyConfig2Add(entityModels[0], sysConfig);
            } else {
                sysConfig.setCstCreate(now);
                sysConfig.setCstModify(now);
                sysConfig.setDelFlag(DELETED.DELETED_NO.getValue());
            }
            sysConfigDao.add(sysConfig);
        }
    }

    @Override
    @WhTransactionalControl
    public <E> void setModel(PlatEnum.SysConfigEnum key, E value, EntityModel... entityModels) throws Exception {
        if (value == null) {
            return;
        }
        Date now = new Date();
        SysConfig checkSysConfig = new SysConfig();
        checkSysConfig.setConfigKey(key.getValue());
        checkSysConfig = sysConfigDao.selectOne(checkSysConfig);
        if (checkSysConfig != null) {
            SysConfig sysConfig = new SysConfig();
            sysConfig.setId(checkSysConfig.getId());
            sysConfig.setConfigValue(JsonUtil.getJsonStr(value));
            if (entityModels != null && entityModels.length > 0 && entityModels[0] != null) {
                EntityCommonCopyUtil.copyConfig2Update(entityModels[0], sysConfig);
            } else {
                sysConfig.setCstModify(now);
            }
            sysConfigDao.updateBySelective(sysConfig);
        } else {
            SysConfig sysConfig = new SysConfig();
            sysConfig.setConfigKey(key.getValue());
            sysConfig.setConfigValue(JsonUtil.getJsonStr(value));
            if (entityModels != null && entityModels.length > 0 && entityModels[0] != null) {
                EntityCommonCopyUtil.copyConfig2Add(entityModels[0], sysConfig);
            } else {
                sysConfig.setCstCreate(now);
                sysConfig.setCstModify(now);
                sysConfig.setDelFlag(DELETED.DELETED_NO.getValue());
            }
            sysConfigDao.add(sysConfig);
        }
    }

    /**
     * 
     * @Title: getConfigForCaches
     * @Description: 获取所有的配置值，用于缓存
     * @author ZhangJianyang
     * @return
     */
    @Override
    public List<ConfigModel> getConfigForCaches() {
        return this.getDao().selectListCustom(null, "getConfigForCaches");
    }
}
